package com.ych.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ych.dao.YonghuDao;
import com.ych.pojo.LoginYonghu;
import com.ych.pojo.Yonghu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Service
public class YonghuService implements UserDetailsService {

    @Autowired
    private YonghuDao yonghuDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
       // 查询用户信息
        Yonghu yonghu = yonghuDao.selectOne(new QueryWrapper<Yonghu>().eq("yname",username) );
        System.out.println("查询数据库");
        System.out.println(yonghu.getYpsw());
        if(Objects.isNull(yonghu)){
            throw new RuntimeException("用户名或密码错误dd");
        }
                // todo 查询对应的权限
        //TODO (授权，即查询用户具有哪些权限)查询对应的用户信息
        //定义一个权限集合
        List<String> list = new ArrayList<String>(Arrays.asList("test","admin"));
// 把用户封装成UserDetails返回
        return new LoginYonghu(yonghu,list);
    }
}
